var LODOP; //声明为全局变量 
var readPersonSetting = {
		check: {
			enable: true,
			chkboxType: {"Y":"", "N":""}
		},
		view: {
			dblClickExpand: false
		},
		data: {
			simpleData: {
				enable: true
			}
		},
		callback: {
			beforeClick: beforeClick,
			onCheck: onCheck
		}
	};
var unitSetting = {
		check: {
			enable: true
		},
		async: {
			enable: true,
			url: getUrl
		},
		data: {
			simpleData: {
			  enable: true
		    }
		},
		view: {
			dblClickExpand: false
		},
		callback: {
			beforeExpand: beforeExpand,
			onAsyncSuccess: onAsyncSuccess,
			onAsyncError: onAsyncError,
			beforeClick: beforeUnitClick,
			onCheck: onCheckUnit
		}
	};
function query(){
	var metertype=$("#metertype").val();
	var formobj=$("#findForm");
	if(metertype=='water'){
		formobj.attr("action","../dashboard/queryCopyForm_Watermeter.action");
	}else{
		formobj.attr("action","../dashboard/queryCopyForm_Anmeter.action");
	}
	formobj.submit();
}
/*初始抄表人员，抄表单元数据*/
function init(){
	/*初始抄表人员*/
	$.post("../base/printLoad_ReadPerson.action",{},function(result){
		$.fn.zTree.init($("#readPersonTree"), readPersonSetting, result.readPersons);
		/*初始勾选抄表员默认值*/
		var personid=$("#personid").val();
		var ids=personid.split(",");
		var readPersonZTree = $.fn.zTree.getZTreeObj("readPersonTree");
		
		for(var i=0;i<ids.length;i++){
			//alert(ids[i]);
			var node=readPersonZTree.getNodeByParam( "id",ids[i] );
			if(node!=null && node!=undefined){
				readPersonZTree.checkNode(node, true, true);
			}
		}
	});
	/*初始抄表单元*/
	$.post("../base/query_RoomInfo.action",{"urlSign":"printcheck"},function(result){
		$.fn.zTree.init($("#unitTree"), unitSetting, result.communitys);
		/*初始勾选抄表单元默认值*/
		var unitid=$("#unitid").val();
		var ids=unitid.split(",");
		var unitTree = $.fn.zTree.getZTreeObj("unitTree");
		for(var i=0;i<ids.length;i++){
			//alert(ids[i]);
			var node=unitTree.getNodeByParam( "id",ids[i] );
			if(node!=null && node!=undefined){
				unitTree.checkNode(node, true, true);
			}
		}
	});
}
/*抄表单元树形结构函数---开始*/
function getUrl(treeId, treeNode) {
	var sign=treeNode.id.substr(0,1);
	var cid=treeNode.id.substr(1);
	var checksign=treeNode.checked;
	if(sign=='C'){
		return "../base/queryTree_Ban.action?cid="+cid+"&urlSign=printcheck&checksign="+checksign;
	}else if(sign=="B"){
		return "../base/queryTree_Unit.action?bid="+cid+"&urlSign=printcheck&checksign="+checksign;
	}
};
function beforeExpand(treeId, treeNode) {
	if (!treeNode.isAjaxing) {
		ajaxGetNodes(treeNode, "refresh");
		return true;
	} else {
		alert("正在下载数据中，请稍后展开节点。。。");
		return false;
	}
};
function ajaxGetNodes(treeNode, reloadType) {
	var zTree = $.fn.zTree.getZTreeObj("unitTree");
	if (reloadType == "refresh") {
		treeNode.icon = "../script/ztree/css/zTreeStyle/img/loading.gif";
		zTree.updateNode(treeNode);
	}
	zTree.reAsyncChildNodes(treeNode, reloadType, true);
};
function onAsyncError(event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
	var zTree = $.fn.zTree.getZTreeObj("unitTree");
	alert("异步获取数据出现异常。");
	treeNode.icon = "";
	zTree.updateNode(treeNode);
};
function onAsyncSuccess(event, treeId, treeNode, msg) {
	var zTree = $.fn.zTree.getZTreeObj("unitTree"),
	totalCount = treeNode.count;
	treeNode.icon = "";
	zTree.updateNode(treeNode);
	zTree.selectNode(treeNode.children[0]);
};
function showUnitMenu() {
	var unitObj = $("#unitSel");
	var unitOffset = $("#unitSel").offset();
	$("#unitContent").css({left:unitOffset.left + "px", top:unitOffset.top + unitObj.outerHeight() + "px"}).slideDown("fast");
    $("body").bind("mousedown", onBodyUnitDown);
}
function hideUnitMenu() {
	$("#unitContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyUnitDown);
}
function beforeUnitClick(treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("unitTree");
	zTree.checkNode(treeNode, !treeNode.checked, null, true);
	return false;
}

function onCheckUnit(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("unitTree"),
	nodes = zTree.getCheckedNodes(true),
	v = "";
	var id="";
	for (var i=0, l=nodes.length; i<l; i++) {
		v += nodes[i].name + ",";
		id += nodes[i].id + ",";
	}
	if (v.length > 0 ){
		v = v.substring(0, v.length-1);
		id = id.substring(0, id.length-1);
	}
	var unitObj = $("#unitSel");
	unitObj.attr("value", v);
	$("#unitid").val(id);
}
function onBodyUnitDown(event) {
	if (!(event.target.id == "unitBtn" || event.target.id == "unitSel" || event.target.id == "unitContent" || $(event.target).parents("#unitContent").length>0)) {
		hideUnitMenu();
	}
}
/*抄表单元树形结构函数---结束*/
/*抄表人员树形结构函数---开始*/
function showMenu() {
	
	var cityObj = $("#citySel");
	var cityOffset = $("#citySel").offset();
	$("#menuContent").css({left:cityOffset.left + "px", top:cityOffset.top + cityObj.outerHeight() + "px"}).slideDown("fast");
    $("body").bind("mousedown", onBodyDown);
}
function hideMenu() {
	$("#menuContent").fadeOut("fast");
	$("body").unbind("mousedown", onBodyDown);
}
function beforeClick(treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("readPersonTree");
	zTree.checkNode(treeNode, !treeNode.checked, null, true);
	return false;
}

function onCheck(e, treeId, treeNode) {
	var zTree = $.fn.zTree.getZTreeObj("readPersonTree"),
	nodes = zTree.getCheckedNodes(true),
	v = "";
	var id='';
	for (var i=0, l=nodes.length; i<l; i++) {
		v += nodes[i].name + ",";
		id+=nodes[i].id+',';
	}
	if (v.length > 0 ){
		v = v.substring(0, v.length-1);
		id = id.substring(0, id.length-1);
	}
	var cityObj = $("#citySel");
	cityObj.attr("value", v);
	$("#personid").val(id);
}
function onBodyDown(event) {
	if (!(event.target.id == "menuBtn" || event.target.id == "citySel" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
		hideMenu();
	}
}
/*抄表人员树形结构函数---结束*/
$(document).ready(function(){
	init();
	
});
function printeach(){
	var lis=$("#myTab").children();
	if(lis.length<1){
		alert("请首先进行查询操作，然后再打印.");
		return;
	}
	var sign=0;
	var ids=new Array();
	for(var i=0;i<lis.length;i++){
		var li_id='';
		li_id=$(lis[i]).find("A").attr("href").substr(1);
		ids[i]=li_id;
		}
	prn_preview(ids);
}
function prn_preview(ids) {	
	CreateOneFormPage(ids);	
	return LODOP.PREVIEW();	
}
function CreateOneFormPage(ids){
	LODOP=getLodop();  
	LODOP.SET_PRINT_STYLE("FontSize",18);
	LODOP.SET_PRINT_STYLE("Bold",1);
	for(var i=0;i<ids.length;i++){
		LODOP.ADD_PRINT_HTM(25,90,"100%","100%",document.getElementById(ids[i]).innerHTML);
		LODOP.NewPageA();
	}
};